package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.QueryCommisionLogsBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.QueryCommisionLogsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*查询佣金更改记录
*FreeMaker Template自动生成代码
*/
@TestSuite
@Ignore
public class Ts_QueryCommisionLogs{
	private static QueryCommisionLogsBean querycommisionlogsBean;
	private static QueryCommisionLogsCall querycommisionlogsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("globalSellerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询佣金更改记录(全球优选业务下架)");
	}

	@Before
	public void caseUp() {
		querycommisionlogsBean=new QueryCommisionLogsBean();
		querycommisionlogsCall=new QueryCommisionLogsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querycommisionlogsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCommisionLogs_001() {
		Logger.start(true,"查询佣金更改记录,平台佣金");
		try {
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			BigDecimal commission = new BigDecimal(10);
			ProductManagerCallServiceV2.UpdateProductCommision(addSellerProductRequest.getSellerId(),productId,addSellerProductRequest.getSellerName(),commission);
			querycommisionlogsBean.setProductId(productId);
			querycommisionlogsCall.setData(querycommisionlogsBean);
			querycommisionlogsCall.callService();
			checkCommisionLogs(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_QueryCommisionLogs_002() {
        Logger.start(true,"查询佣金更改记录,销售佣金");
        try {
            AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
            String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
            BigDecimal commission = new BigDecimal(10);
            String catalogId = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
            ProductManagerCallServiceV2.UpdateCatalogCommisions(addSellerProductRequest.getSellerId(),productId,catalogId,addSellerProductRequest.getSellerName(),commission);
            querycommisionlogsBean.setProductId(productId);
            querycommisionlogsCall.setData(querycommisionlogsBean);
            querycommisionlogsCall.callService();
            Logger.verifyEquals("200",querycommisionlogsCall.getString("Code"),"验证Code");
            checkCommisionLogs(productId);
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCommisionLogs_004() {
		Logger.start(true,"查询佣金更改记录,优选团商品,销售佣金");
		try {
			AddSellerProductRequest addSellerProductRequest = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId,productId,2);

			List<String> catalogIds = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			ProductManagerCallServiceV2.UpdateCatalogCommisionsV2(sellerId,productId,catalogIds.get(0),addSellerProductRequest.getSellerName(), Arrays.asList(new BigDecimal(7),new BigDecimal(8),new BigDecimal(9),new BigDecimal(10)));

			Thread.sleep(2000);
			querycommisionlogsBean.setProductId(productId);
			querycommisionlogsCall.setData(querycommisionlogsBean);
			querycommisionlogsCall.callService();
			Logger.verifyEquals("200",querycommisionlogsCall.getString("Code"),"验证Code");
			checkCommisionLogs(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryCommisionLogs_003() {
		Logger.start(true,"查询佣金更改记录，商品id不存在");
		try {
			querycommisionlogsBean.setProductId("qjhfjwq");
			querycommisionlogsCall.setData(querycommisionlogsBean);
			querycommisionlogsCall.callService();
			Logger.verifyEquals("200",querycommisionlogsCall.getString("Code"),"验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

    public void checkCommisionLogs(String productId){
	    Map map = new YmtProductsIWapper().selectCommissionLogByProductId(productId).get(0);
        querycommisionlogsCall.getCommissionLogs().forEach(p->{
            String changeValue = map.get("ChangeValue").toString();
            Logger.verifyEquals(p.getChangeValue(),changeValue,"验证更改的佣金值");
            Logger.verifyEquals(p.getCatalog(),map.get("Catalog").toString(),"验证规格");
            Logger.verifyEquals(p.getLogType(),Integer.parseInt(map.get("LogType").toString()),"验证LogType");
            String date = map.get("OperateTime").toString();
            try {
                date = YMTDateUtil.convertDateStrToAnotherFormat(date,YMTDateUtil.YMDHMSS,YMTDateUtil.YMDHMS).toString();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Logger.verifyEquals(p.getOperateTime(),date,"验证OperateTime");
            Logger.verifyEquals(p.getOperatorName(),map.get("OperatorName").toString(),"验证OperatorName");
        });
    }
}